package com.nf.util;

import java.sql.*;

public class DBUtil {

    //数据库5大参数

    private static final String IP = "127.0.0.1";
    private static final String PORT = "3306";
    private static final String DATABASE_NAME = "noticeDB";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123";
    private static final String DRIVER = "org.gjt.mm.mysql.Driver";

    public static Connection getConnection() throws Exception {
        String url = "jdbc:mysql://"+IP+":"+PORT+"/"+DATABASE_NAME+"?user="+USER_NAME+"&password="+PASSWORD+"";
        Class.forName(DRIVER);
        Connection conn = DriverManager.getConnection(url);
        //System.out.println("ConnManager已经创建了连接："+conn);
        return conn;
    }
    //根据题意，增加多一个关闭方法
    public static void close(Connection conn){
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(PreparedStatement pst){
        try {
            pst.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs){
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
